package com.fpt.edu.vn.h2tv.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.ITbFeedbackDAO;
import com.fpt.edu.vn.h2tv.dao.TbFeedbackDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbFeedback;
import com.fpt.edu.vn.h2tv.dto.Feedback;
import com.fpt.edu.vn.h2tv.dto.FeedbackResponse;

public class FeedbackService implements IBaseService {

    @Override
    public Object getResponse() {
        return getFeedbackResponse();
    }
    /**
     * Get all category Return to client
     * @return CategoryResponse
     */
    private FeedbackResponse getFeedbackResponse() {
        ITbFeedbackDAO dao = new TbFeedbackDAO();
        // find all category on database
        List<TbFeedback> tbFeedbackList = dao.findAll();
        FeedbackResponse response = new FeedbackResponse();
        List<Feedback> feedbackList = new ArrayList<Feedback>();
        if (tbFeedbackList != null && tbFeedbackList.size() > 0) {
            for (TbFeedback feedback : tbFeedbackList) {
                Feedback feedbackDTO = new Feedback();
                feedbackDTO.setFeedbackId(feedback.getFeedBackId().intValue());
                feedbackDTO.setEmail(feedback.getEmail());
                feedbackDTO.setTitle(feedback.getTitle());
                feedbackDTO.setContents(feedback.getContents());
                feedbackDTO.setIsReaded(feedback.getIsReaded().booleanValue());
                feedbackDTO.setCreateTime(feedback.getCreateTime().toString());
                feedbackList.add(feedbackDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setFeedback(feedbackList);
        return response;
    }

    public static String updateFeedback(int id) {
        Boolean isReaded = true;
        ITbFeedbackDAO feedbackDAO = new TbFeedbackDAO();
        TbFeedback feedback = feedbackDAO.findById(id);
        String success;
        if (feedback != null) {
            // Transaction
            feedback.setIsReaded(isReaded);
            EntityManagerHelper.beginTransaction();
            feedbackDAO.update(feedback);
            EntityManagerHelper.commit();
            success = "update feedback " + id + " successful!";
        } else {
            success = "update feedback " + id + " failed!";
        }

        return success;
    }

    public static String deleteFeedback(int id) {
        ITbFeedbackDAO feedbackDAO = new TbFeedbackDAO();
        TbFeedback feedback = feedbackDAO.findById(id);
        String success;
        if (feedback != null) {
            // Transaction
            EntityManagerHelper.beginTransaction();
            feedbackDAO.delete(feedback);
            EntityManagerHelper.commit();
            success = "delete category " + id + " successful!";
        } else {
            success = "delete category " + id + " failed!";
        }

        return success;
    }

    public static FeedbackResponse searchFeedback(String key) {
        ITbFeedbackDAO feedbackDAO = new TbFeedbackDAO();
        List<TbFeedback> tbFeedbackList = feedbackDAO.findbyEmail(key);
        FeedbackResponse response = new FeedbackResponse();
        List<Feedback> feedbackList = new ArrayList<Feedback>();
        if (tbFeedbackList != null && tbFeedbackList.size() > 0) {
            for (TbFeedback feedback : tbFeedbackList) {
                Feedback feedbackDTO = new Feedback();
                feedbackDTO.setFeedbackId(feedback.getFeedBackId().intValue());
                feedbackDTO.setEmail(feedback.getEmail());
                feedbackDTO.setTitle(feedback.getTitle());
                feedbackDTO.setContents(feedback.getContents());
                feedbackDTO.setIsReaded(feedback.getIsReaded().booleanValue());
                feedbackDTO.setCreateTime(feedback.getCreateTime().toString());
                feedbackList.add(feedbackDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setFeedback(feedbackList);
        return response;
    }

    public static FeedbackResponse filterReaded() {
        ITbFeedbackDAO feedbackDAO = new TbFeedbackDAO();
        List<TbFeedback> tbFeedbackList = feedbackDAO.filterReaded();
        FeedbackResponse response = new FeedbackResponse();
        List<Feedback> feedbackList = new ArrayList<Feedback>();
        if (tbFeedbackList != null && tbFeedbackList.size() > 0) {
            for (TbFeedback feedback : tbFeedbackList) {
                Feedback feedbackDTO = new Feedback();
                feedbackDTO.setFeedbackId(feedback.getFeedBackId().intValue());
                feedbackDTO.setEmail(feedback.getEmail());
                feedbackDTO.setTitle(feedback.getTitle());
                feedbackDTO.setContents(feedback.getContents());
                feedbackDTO.setIsReaded(feedback.getIsReaded().booleanValue());
                feedbackDTO.setCreateTime(feedback.getCreateTime().toString());
                feedbackList.add(feedbackDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setFeedback(feedbackList);
        return response;
    }

    public static FeedbackResponse filterUnReaded() {
        ITbFeedbackDAO feedbackDAO = new TbFeedbackDAO();
        List<TbFeedback> tbFeedbackList = feedbackDAO.filterUnReaded();
        FeedbackResponse response = new FeedbackResponse();
        List<Feedback> feedbackList = new ArrayList<Feedback>();
        if (tbFeedbackList != null && tbFeedbackList.size() > 0) {
            for (TbFeedback feedback : tbFeedbackList) {
                Feedback feedbackDTO = new Feedback();
                feedbackDTO.setFeedbackId(feedback.getFeedBackId().intValue());
                feedbackDTO.setEmail(feedback.getEmail());
                feedbackDTO.setTitle(feedback.getTitle());
                feedbackDTO.setContents(feedback.getContents());
                feedbackDTO.setIsReaded(feedback.getIsReaded().booleanValue());
                feedbackDTO.setCreateTime(feedback.getCreateTime().toString());
                feedbackList.add(feedbackDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setFeedback(feedbackList);
        return response;
    }

    public static Object getFeedbackById(int id) {
        ITbFeedbackDAO feedbackDAO = new TbFeedbackDAO();
        TbFeedback tbFeedback = feedbackDAO.findById(id);
        FeedbackResponse response = new FeedbackResponse();
        Feedback feedbackDTO = new Feedback();
        if (tbFeedback != null) {
            feedbackDTO.setFeedbackId(tbFeedback.getFeedBackId().intValue());
            feedbackDTO.setEmail(tbFeedback.getEmail());
            feedbackDTO.setTitle(tbFeedback.getTitle());
            feedbackDTO.setContents(tbFeedback.getContents());
            feedbackDTO.setIsReaded(tbFeedback.getIsReaded().booleanValue());
            feedbackDTO.setCreateTime(tbFeedback.getCreateTime().toString());

            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        
        return feedbackDTO;
    }
    public static String addFeedBack(String email, String title, String contents){
        TbFeedbackDAO dao = new TbFeedbackDAO();
        TbFeedback feedback = new TbFeedback();
        feedback.setEmail(email);
        feedback.setTitle(title);
        feedback.setContents(contents);
        feedback.setIsReaded(false);
        feedback.setCreateTime(new Timestamp(System.currentTimeMillis()));
        EntityManagerHelper.beginTransaction();
        dao.save(feedback);
        EntityManagerHelper.commit();
        return "Add oke";
    }
}
